import React from 'react';
import { Route, Redirect } from 'react-router-dom';
import { deciphering, encryption } from '../util/Util.js';
const QuestionRoute = ({ component: Component, ...rest }) => {
  // 从localStorage获取并解析userInfo
  const userInfo = JSON.parse(deciphering(localStorage.getItem("userInfo")));
  // 如果role为2，设置shouldRedirect为true
  const shouldRedirect = userInfo.role === 2;
  return (
    <Route
      {...rest}
      render={props =>
        // 如果shouldRedirect为true，跳转到"/question/train"，否则渲染原来的组件
        shouldRedirect ? (
          <Redirect to={"/question/train"} />
        ) : (
          (userInfo.role && (rest.path === "/handpaper" || rest.path === "/question/newquestion")) ? (<Redirect to={"/question/question"} />) : (<Component {...props} />)

        )
      }
    />
  );
};

export default QuestionRoute;
